﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CardLogistics.Web.Components;
using CardLogistics.Model.Basic;

namespace CardLogistics.Web.MainPage.Basic.BasicType
{
    public partial class AddEdit : BasePage
    {
        private static readonly CardLogistics.DAL.Basic.Basic_BasicTypeData DAL = new DAL.Basic.Basic_BasicTypeData();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Mid == "100204")//芯片类型
            {
                lb_CHIPMODEL.Visible = true;
                CHIPMODEL.Visible = true;
            }else
            {
                lb_CHIPMODEL.Visible = false;
                CHIPMODEL.Visible = false;
            }
            if (!IsPostBack)
            {
                BindTxt();
            }
        }

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string result = "";
                string logContent = "";
                string typeNO = "";//默认为 卡芯片类型 信息 芯片类型的菜单id=10024 采购类型菜单id=10025  入库类型菜单id=10026
                if (Mid == "10024")
                    typeNO = "卡芯片类型";
                else if (Mid == "10025")
                    typeNO = "采购类型";//采购类型 信息
                else if (Mid == "10026")
                    typeNO = "入库类型";//入库类型 信息
                if (IsEdit)
                {
                    result = DAL.Update(EncapsulationModel());
                    logContent = "修改" + typeNO + ",名称:" + EncapsulationModel().TYPENAME;
                }
                else
                {
                    result = DAL.Add(EncapsulationModel());
                    logContent = "修改" + typeNO + ",名称:" + EncapsulationModel().TYPENAME;
                }

                if (result.Split(',')[0] == "ok")
                {
                    InsertLog(logContent);
                    ReturnListInfo();
                }
                else if (result.Split(',')[1] == "-1")
                {
                    ShowMessageBox("类型名称重复,请重新输入！");
                    TYPENAME.Focus();
                }
                else
                {
                    ShowMessageBox("操作失败！" + result.Split(',')[1]);
                }
            }
            catch (Exception ex)
            {
                ShowMessageBox("系统异常:" + ex.ToString());
            }
        }
        /// <summary>
        /// 获取对象
        /// </summary>
        /// <returns></returns>
        private Basic_BasicType EncapsulationModel()
        {
            Basic_BasicType model = new Basic_BasicType();
            model = GetWebControls<Basic_BasicType>(this.Page);
            model.LASTUPDATEUSER = LoggedUser.ID;
            if (model.STATUS == null)
                model.STATUS = 0;
            model.ISDELETE = 0;
            if (Mid == "100204")//芯片类型
                model.TYPENO = 0;
            else if (Mid == "100205")//采购类型
                model.TYPENO = 1;
            else if (Mid == "100206")//入库类型
                model.TYPENO = 2;
            if (IsEdit)
                model.ID = Request.QueryString["id"];
            return model;
        }
        /// <summary>
        /// 绑定值
        /// </summary>
        private void BindTxt()
        {
            string id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                Basic_BasicType Data = DAL.GetModel(id);
                SetWebControls(this.Page, Data);
                if (Data != null)
                {
                    if (Data.STATUS==2)//系统参数（不可修改删除）
                    {
                        this.btn_Add.Visible = false; 
                    }
                }
            }
        }
    }
}